from flask import Flask,request,jsonify
import json
from flask_cors import CORS
from dbs import dbs


app = Flask(__name__)
CORS(app)
@app.route('/order',methods=['POST','GET','DELETE'])
def order():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        wuliu = ''
        if int(data['status']) == 3:
            wuliu = '已出库'
        else:
            wuliu = '没有物流信息'

        sql = 'insert into p7lx values(0,"%s",%d,"%s")'%(data['name'],int(data['status']),wuliu)
        dbs.update(sql)
        return jsonify({'code':200})
    elif request.method == 'GET':
        cpage = int(request.args.get('cpage'))
        page_size = int(request.args.get('page_size'))
        start = (cpage-1)*page_size
        sql = 'select * from p7lx order by id asc limit %d,%d'%(start,page_size)
        res = dbs.findall(sql)
        sql2 = 'select count(id) from p7lx'
        total = dbs.findone(sql2)
        return jsonify({'code':200,'list':res,'total':total['count(id)']})
    elif request.method == 'DELETE':
        id = request.args.get('id')
        sql = 'delete from p7lx where id = %d '%(int(id))
        dbs.update(sql)
        return jsonify({'code':200})
@app.route('/order1',methods=['DELETE'])
def order1():
    if request.method == 'DELETE':
        dlist = request.args.get('dlist')
        sql = 'delete from p7lx where id in (%s)'%(dlist)
        dbs.update(sql)
        return jsonify({'code':200})
if __name__ == '__main__':
    app.run(debug=True)